/**
 * 
 */
package denaris.factory;

import java.io.IOException;
import java.util.Hashtable;

import javax.microedition.lcdui.Image;

import denaris.midlet.Denaris;

/**
 * @author stkl23
 * 
 */
public class ImageFactory {
	private static Hashtable cache = new Hashtable(25);

	/**
	 * Returns the image at the given path. The image is first looked up in the
	 * internal cache and either returned from there or loaded physically.
	 * 
	 * @param path
	 * @return
	 */
	public static Image getImage(String path) {
		Image result = null;
		// check the cache
		if (cache.containsKey(path)) {
			result = (Image) cache.get(path);
		}else {
			try {
				// load the image
				Image img = Image.createImage(path);
				// add it to the cache
				cache.put(path, img);
				result = img;
			} catch (IOException e) {
				Denaris.getInstance().handleException(e);
			}
		}
		return result;
	}
}
